package com.itany.time;

import com.alibaba.fastjson.JSON;
import com.itany.vo.TreeVo;
import com.itany.webstock.WebSocketServer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OnlineNumbers implements Runnable {

    @Override
    public void run() {
        try {

           while (true){
               Thread.sleep(300);
               List<TreeVo> list = new ArrayList<TreeVo>();
               for(WebSocketServer server:WebSocketServer.webSocketSet){
                   TreeVo vo=new TreeVo();
                   vo.setText(server.getUserName());
                   list.add(vo);
               }

               Map<String,Object> map = new HashMap<String,Object>();
               map.put("flag",2);
               map.put("msg",list);
               for(WebSocketServer server:WebSocketServer.webSocketSet){
                   synchronized (server.getSession()){
                       //将字符串信息推送给了session对应的界面
                       server.getSession().getBasicRemote().sendText(JSON.toJSONString(map));
                   }
               }
           }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
